package com.unionpay.upyzt.example;

import com.unionpay.upyzt.Upyzt;
import com.unionpay.upyzt.exception.UpyztException;
import com.unionpay.upyzt.model.CombinedDepositConfirm;
import com.unionpay.upyzt.resp.CombinedDepositConfirmResp;

import java.time.OffsetDateTime;
import java.util.HashMap;
import java.util.Map;

public class CombinedDepositConfirmExample {

    private final String guaranteeDepositId;

    public CombinedDepositConfirmExample(String guaranteeDepositId) {
        this.guaranteeDepositId = guaranteeDepositId;
    }

    /**
     * 运行 Demos
     *
     * @param guaranteeDepositId
     * @return 合并支付担保确认ID
     */
    public static String runDemos(String guaranteeDepositId) throws UpyztException {
        CombinedDepositConfirmExample combinedDepositConfirmExample = new CombinedDepositConfirmExample(guaranteeDepositId);

        // 合并支付担保确认
        CombinedDepositConfirmResp combinedDepositConfirmResp = combinedDepositConfirmExample.create();

        // 合并支付担保确认订单查询（系统订单号）
        combinedDepositConfirmResp = combinedDepositConfirmExample.retrieveById(combinedDepositConfirmResp.getCombinedDepositConfirmId());

        // 合并支付担保确认订单查询（平台订单号）
        combinedDepositConfirmResp = combinedDepositConfirmExample.retrieveByOutOrderNo(combinedDepositConfirmResp.getOutOrderNo());

        return combinedDepositConfirmResp.getCombinedDepositConfirmId();
    }

    /**
     * 合并支付担保确认
     *
     * @return 返回参数
     * @throws UpyztException 异常
     */
    public CombinedDepositConfirmResp create() throws UpyztException {
        Map<String, Object> params = new HashMap<>();
        params.put("guarantee_deposit_id", this.guaranteeDepositId);
        params.put("out_order_no", Main.generateRandomOutNo());
        params.put("sent_at", OffsetDateTime.now().toString());
        params.put("amount", 5);
        params.put("password", Upyzt.encryptField("pa55w0rd"));
        params.put("remark", "用于 SDK 示例测试");
        params.put("metadata", MockData.metadata());
        params.put("extra", MockData.extra());
        System.out.println("------- 合并支付担保确认 -------");
        CombinedDepositConfirmResp resp = CombinedDepositConfirm.create(params);
        System.out.println(resp);
        return resp;
    }

    /**
     * 合并支付担保确认订单查询（系统订单号）
     *
     * @return 返回参数
     * @throws UpyztException 异常
     */
    public CombinedDepositConfirmResp retrieveById(String id) throws UpyztException {
        System.out.println("------- 合并支付担保确认订单查询（系统订单号） -------");
        CombinedDepositConfirmResp resp = CombinedDepositConfirm.retrieveById(id);
        System.out.println(resp);
        return resp;
    }

    /**
     * 合并支付担保确认订单查询（平台订单号）
     *
     * @return 返回参数
     * @throws UpyztException 异常
     */
    public CombinedDepositConfirmResp retrieveByOutOrderNo(String outOrderNo) throws UpyztException {
        System.out.println("------- 合并支付担保确认订单查询（平台订单号） -------");
        CombinedDepositConfirmResp resp = CombinedDepositConfirm.retrieveByOutOrderNo(outOrderNo);
        System.out.println(resp);
        return resp;
    }

}
